package com.gcxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gcxy.dto.Pen.InsertDto;
import com.gcxy.dto.Pen.UpdateDto;
import com.gcxy.entity.Pen;
import com.gcxy.entity.Ranch;
import com.gcxy.mapper.PenMapper;
import com.gcxy.mapper.RanchMapper;
import com.gcxy.service.PenService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcxy.utils.R;
import com.gcxy.vo.Pen.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wlp
 * @since 2023-12-19
 */
@Service
public class PenServiceImpl extends ServiceImpl<PenMapper, Pen> implements PenService {

    @Autowired
    private PenMapper penMapper;

    @Autowired
    private RanchMapper ranchMapper;
    @Override
    public R addPen(InsertDto requestBody) {
        LambdaQueryWrapper<Pen> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Pen::getPenName,requestBody.getPenName())
                .eq(Pen::getRanchId,requestBody.getRanchId());
//        LambdaQueryWrapper<Ranch> queryWrapper1=new LambdaQueryWrapper<>();
//        queryWrapper1.eq(Ranch::getRanchId,requestBody.getRanchId())
//                .eq(Ranch::getRanchState,1);
//        Ranch ranch = ranchMapper.selectOne(queryWrapper1);
//        if(ranch==null){
//            return R.Failed(0,"牧场名不存在");
//        }
        Pen pen = penMapper.selectOne(queryWrapper);
        if(pen!=null){
            return R.Failed(0,"圈舍名称已存在");
        }
        Pen pen1=Pen.builder()
                .penName(requestBody.getPenName())
                .penState(1)
                .penAmount(0)
                .penName(requestBody.getPenName())
                .ranchId(requestBody.getRanchId().intValue())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        int insert = penMapper.insert(pen1);
        if (insert>0){
            return R.Success();
        }
        return R.Failed(0,"新增失败");
    }

    @Override
    public R pagePen(PageVo requestBody) {
        Page<PageVo> page=new Page<>(requestBody.getPageNumber(),requestBody.getPageSize());
        QueryWrapper<PageVo> queryWrapper=new QueryWrapper<>();
            queryWrapper.like(requestBody.getRanchName()!=null&&!"".equals(requestBody.getRanchName()),"r.ranch_name",requestBody.getRanchName())
                    .like(requestBody.getPenName()!=null&&!"".equals(requestBody.getRanchName()),"p.pen_name",requestBody.getPenName())
                    .eq(requestBody.getPenState()!=null,"p.pen_state",requestBody.getPenState())
                    .orderByDesc("create_time");
        penMapper.selectPenPage(page,queryWrapper);
        return R.Success(page);
    }

    @Override
    public R penUpdate(UpdateDto requestBody) {
        LambdaQueryWrapper<Pen> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Pen::getRanchId,requestBody.getRanchId())
                .eq(Pen::getPenName,requestBody.getPenName());
        Pen pen = penMapper.selectOne(queryWrapper);
        if(pen!=null&&!pen.getPenId().equals(requestBody.getPenId())){
            return R.Failed(0,"圈舍名已存在");
        }
        Pen pen1=Pen.builder()
                .penId(requestBody.getPenId())
                .penName(requestBody.getPenName())
                .ranchId(pen.getRanchId())
                .build();
        int i = penMapper.updateById(pen1);
        if(i>0){
            return R.Success();
        }
        return R.Failed(0,"修改失败");
    }

    @Override
    public R penUpdateState(Integer penId, Integer penState) {
        //前端判断
//        Pen pen = penMapper.selectById(penId);
//        if(pen==null){
//            return R.Failed(0,"圈舍不存在");
//        }
        Pen pen1=Pen.builder()
                .penId(penId)
                .penState(penState)
                .build();
        int i = penMapper.updateById(pen1);
        if(i>0){
            return R.Success();
        }
        return R.Failed(0,"修改圈舍状态失败");
    }

    @Override
    public R getPenByRanchId(Integer ranchId) {
        LambdaQueryWrapper<Pen> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Pen::getRanchId,ranchId);
        List<Pen> pens = penMapper.selectList(queryWrapper);
        return R.Success(pens);
    }
}
